package com.example.day01.m;

import android.util.Log;

import com.example.day01.ApiService;
import com.example.day01.CallBack;
import com.example.day01.DataBean;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainModel {
    public void getData(final CallBack callBack)  {
        new Retrofit.Builder()
                .baseUrl(ApiService.strURL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .databean()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DataBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DataBean dataBean) {
                    callBack.showData(dataBean.getResults());
                        Log.i("1",dataBean.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                    callBack.show(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
}
